<?php

use App\Http\Controllers\PostController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Validation\ValidationException;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::view('/form','form');

//处理表单提交
Route::post('/post',function(Request $request){
    $data = $request->validate([
        'title'=> 'required',
        'author'=> 'required',
        'content'=> 'required',
    ]);

    return response()->json($data);
});
//返回用户登录
Route::get('/user', function() {
    // return auth()->check() ? auth()->user() : [];
    return response()->json(['id'=>1,'name'=>'肖吉彬']);
});

Route::view('/login-form','login');
Route::post('/user/login', function(Request $request){
    $request->validate([
        'email'=>'required|email',
        'password'=>'required'
    ]);

    $email = $request->input('email');
    $password = $request->input('password');
    if($email=='test@xueyuanjun.com' && $password=='123456'){
        return  response()->json(['success'=>true,'message'=>'用户登录成功']);
    }

    throw ValidationException::withMessages(['email'=>['邮箱密码不匹配']]);
});

// Route::get('/posts', function(){
//     return view('posts');
// });


// Route::get('/get_posts',function(){
//     return [
//         [
//             'id'=>1,
//             'url'=>url('/post/1'),
//             'title'=>'测试文章1',
//             'created_at'=>'2019-10-15 17:51:32',
//             'status'=>0,
//         ],
//         [
//             'id'=>2,
//             'url'=>url('/post/2'),
//             'title'=>'测试文章2',
//             'created_at'=>'2020-01-21 15:53:06',
//             'status'=>1,
//         ],
//         [
//             'id'=>3,
//             'url'=>url('/post/3'),
//             'title'=>'测试文章3',
//             'created_at'=>'2020-09-07 10:31:06',
//             'status'=>0,
//         ],
//         [
//             'id'=>4,
//             'url'=>url('/post/4'),
//             'title'=>'测试文章4',
//             'created_at'=>'2020-05-17 01:29:24',
//             'status'=>1,
//         ],
//         [
//             'id'=>5,
//             'url'=>url('/post/5'),
//             'title'=>'测试文章5',
//             'created_at'=>'2020-10-20 15:26:43',
//             'status'=>1,
//         ],
//     ];
// });
Route::get('posts/all',[PostController::class,'all']);
Route::get('posts/{post}/data',[PostController::class,'data']);
Route::resource('posts',PostController::class);
